package com.ihunanren.biz.weixin.domain.school;

import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.tags.OptionTag;
import org.htmlparser.tags.SelectTag;
import org.htmlparser.util.NodeList;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.ihunanren.biz.common.HnrResourceLoader;

public class DivisionUniversity {

	public static void main(String[] args) {
		try {
			String text = HnrResourceLoader.getResoureceAsString("renren.dat");
			Country country = JSON.parseObject(text, Country.class);
			int  count = 1;
			for(Province provice : country.getProvs()){
				List<University> univs = provice.getUnivs();
				for(University university : univs){
					List<String> depts = getDepts(university.getId());
					university.setDepts(depts);
					for(String dept:depts){
						count ++;
						System.out.println(count+"-"+provice.getName()+"-"+university.getName()+"-"+dept);
					}
				}
			}
			
			String str = JSON.toJSONString(country);
			FileWriter fileWriter = new FileWriter(new File("D://division_university.dat"));
			fileWriter.write(str);
			fileWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static URLConnection initConnection(String url) throws Exception {
		URL u = new URL(url);
		URLConnection con = u.openConnection();
		con.setAllowUserInteraction(false);
		return con;
	}

	private static List<String> getDepts(int univId) throws Exception {
		List<String> list = Lists.newArrayList();
		String url = "http://www.renren.com/GetDep.do?id=" + univId;
		URLConnection connection = initConnection(url);
		Parser parser = new Parser(connection);

		NodeList nodeList = parser.extractAllNodesThatMatch(new NodeFilter() {
			private static final long serialVersionUID = 1L;

			// 实现该方法,用以过滤标签
			public boolean accept(Node node) {
				if (node instanceof SelectTag) // <A>标记
					return true;
				return false;
			}
		});
		// 打印

		for (int i = 0; i < nodeList.size(); i++) {
			SelectTag n = (SelectTag) nodeList.elementAt(i);

			OptionTag[] ot = n.getOptionTags();
			for (OptionTag tag : ot) {
				StringBuffer sb = new StringBuffer(tag.getOptionText());
				int pos;
				try {
					while ((pos = sb.indexOf("&#")) > -1) {
						String tmp = sb.substring(pos + 2, pos + 7);
						sb.replace(pos, pos + 8, Character.toString((char) Integer.parseInt(tmp, 10)));
					}
				} catch (Exception e) {
					System.out.println(univId+","+tag.getOptionText());
				}
				if("院系".equals(sb.toString()) ){
					continue;
				}
				list.add(sb.toString());
			}

		}
		return list;
	}
}
